package com.et.generator.impl;

import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.et.ar.ActiveRecordBase;
import com.et.ar.ConfReader;
import com.et.ar.adapters.Adapter;
import com.et.ar.connections.ConnectionProvider;
import com.et.generator.ModelProducer;
import com.et.generator.impl.adapter.Executor;
import com.et.generator.impl.adapter.Mysql;
import com.et.generator.impl.adapter.Oracle;

/**
 * <p>模型生产实现</p>
 * @author defier Lai
 * <p>Date: 2009-12-6 下午10:04:21</p>
 * @version 1.0
 */
public class ModelProducerImpl implements ModelProducer {
	
	private String packageName;
	private List<String> tables;
	private Class<?> domainBaseClass;

	@Override
	public void setPackageName(String packageName) {
		this.packageName = packageName;
		
	}
	
	@Override
	public void setTableNames(String tableNames) {
		if(tableNames == null) return;
		tables = new ArrayList<String>();
		String[] ts = tableNames.split(",");
		for(String t : ts) {
			if(t != null && !t.trim().equals("")) {
				tables.add(t.trim());
			}
		}
	}
	
	@Override
	public void setDomainBaseClass(Class<?> domainBaseClass) {
		this.domainBaseClass = domainBaseClass;
	}
	
	@Override
	public void execute() throws Exception {
		initProp(domainBaseClass);
		ConnectionProvider provider = ActiveRecordBase.getConnectionProvider(domainBaseClass);
		Adapter adapter = ActiveRecordBase.getConnectionAdapter(domainBaseClass); // TODO:需要etmvc开放此方法，才能获取到adapter
		Connection conn = provider.getConnection();
		
		String adapterName = adapter.getAdapterName();
		Executor executor = null;
        if (adapterName.equals("mysql")){
        	executor = new Mysql();
        }
        else if (adapterName.equals("oracle")){
            executor = new Oracle();
        }
        else if (adapterName.equals("sqlserver")){
            // TODO 暂时不支持
        }
        else if (adapterName.equals("db2")){
        	// TODO 暂时不支持
        }
		
        if(executor != null) {
        	executor.setConnection(conn);
            executor.setDomainBaseClass(domainBaseClass);
            executor.setPackageName(packageName);
            executor.setTableNames(tables);
        	executor.run();
        }
		
		
	}

	private void initProp(Class<?> domainBaseClass) throws Exception {
		InputStream resource = getClass().getClassLoader().getResourceAsStream("activerecord.properties");
        if (resource == null){
            resource = getClass().getClassLoader().getResourceAsStream("/activerecord.properties");
        }
        if(resource != null) {
        	ConfReader reader = new ConfReader();
            reader.init();
        } else {
        	String path = "spring.xml";
        	resource = getClass().getClassLoader().getResourceAsStream(path);
        	if (resource == null){
        		path = "/spring.xml";
                resource = getClass().getClassLoader().getResourceAsStream(path);
            }
        	if(resource != null) {
        		new ClassPathXmlApplicationContext(path);
        	} else {
        		throw new IllegalArgumentException("activerecord.properties or spring.xml is not exist!");
        	}
        }
	}

}
